package com.ruoyi.ams.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.ams.domain.AmsAssetScrapDetail;
import com.ruoyi.ams.service.IAmsAssetScrapDetailService;
import com.ruoyi.common.core.domain.KeyValueVo;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.security.annotation.PreAuthorize;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 清理报废明细Controller
 * 
 * @author yfkj
 * @date 2022-05-16
 */
@RestController
@RequestMapping("/ams/scrapDetail")
public class AmsAssetScrapDetailController extends BaseController
{
    @Autowired
    private IAmsAssetScrapDetailService amsAssetScrapDetailService;

    /**
     * 查询清理报废明细列表
     */
    @PreAuthorize
    //@PreAuthorize(hasPermi = "ams:detail:list")
    //@ApiOperation(value = "获取清理报废明细列表")
    @GetMapping("/getAmsAssetScrapDetailList")
    public TableDataInfo getAmsAssetScrapDetailList(AmsAssetScrapDetail amsAssetScrapDetail)
    {
        startPage();
        List<AmsAssetScrapDetail> list = amsAssetScrapDetailService.selectAmsAssetScrapDetailList(amsAssetScrapDetail);
        return getDataTable(list);
    }


    /**
     * 获取清理报废明细详细信息
     */
    @PreAuthorize
    //@PreAuthorize(hasPermi = "ams:detail:query")
    @ApiOperation(value = "获取清理报废明细详情")
    @GetMapping(value = "/getAmsAssetScrapDetailById")
    public AjaxResult getAmsAssetScrapDetailById(@RequestParam("scrapId") Long scrapId)
    {
        return AjaxResult.success(amsAssetScrapDetailService
                .getOne(new QueryWrapper<AmsAssetScrapDetail>()
                        .eq(AmsAssetScrapDetail.SCRAP_ID, scrapId), false));
    }

    /**
     * 新增清理报废明细
     */
    @PreAuthorize
    //@PreAuthorize(hasPermi = "ams:detail:add")
    @Log(title = "清理报废明细", businessType = BusinessType.INSERT)
    //@ApiOperation(value = "新增清理报废明细")
    @PostMapping(value = "/addAmsAssetScrapDetail")
    public AjaxResult addAmsAssetScrapDetail(@RequestBody AmsAssetScrapDetail amsAssetScrapDetail)
    {
        return toAjax(amsAssetScrapDetailService.insertAmsAssetScrapDetail(amsAssetScrapDetail));
    }

    /**
     * 修改清理报废明细
     */
    @PreAuthorize
    //@PreAuthorize(hasPermi = "ams:detail:edit")
    @Log(title = "清理报废明细", businessType = BusinessType.UPDATE)
    //@ApiOperation(value = "修改清理报废明细")
    @PostMapping(value = "/editAmsAssetScrapDetail")
    public AjaxResult editAmsAssetScrapDetail(@RequestBody AmsAssetScrapDetail amsAssetScrapDetail)
    {
        return toAjax(amsAssetScrapDetailService.updateAmsAssetScrapDetail(amsAssetScrapDetail));
    }

    /**
     * 删除清理报废明细
     */
    @PreAuthorize
    //@PreAuthorize(hasPermi = "ams:detail:remove")
    @Log(title = "清理报废明细", businessType = BusinessType.DELETE)
	@GetMapping("/removeAmsAssetScrapDetails")
    public AjaxResult removeAmsAssetScrapDetails(@RequestParam Long[] scrapDetailIds)
    {
        return toAjax(amsAssetScrapDetailService.deleteAmsAssetScrapDetailByScrapDetailIds(scrapDetailIds));
    }
    @GetMapping("/getAmsAssetScrapDetailNamesForSelect")
    //@ApiOperation(value = "获取清理报废明细名称列表用于下拉搜索")
    public AjaxResult getAmsAssetScrapDetailNamesForSelect()
    {
        List<AmsAssetScrapDetail> list = amsAssetScrapDetailService.list();//自行加条件
        List<KeyValueVo> resultList = new ArrayList<>();
        for (AmsAssetScrapDetail amsAssetScrapDetail: list ) {
            KeyValueVo vo = new KeyValueVo();
            //vo.setKey(it.getId());
            //vo.setValue(it.getName());
            resultList.add(vo);
        }
        return AjaxResult.success(resultList);
    }
    /**
     * 导出清理报废明细列表
     */
    @PreAuthorize
    //@PreAuthorize(hasPermi = "ams:detail:export")
    @Log(title = "清理报废明细", businessType = BusinessType.EXPORT)
    @PostMapping("/exportAmsAssetScrapDetail")
    public String exportAmsAssetScrapDetail(HttpServletResponse response, AmsAssetScrapDetail amsAssetScrapDetail) throws IOException
    {
        List<AmsAssetScrapDetail> list = amsAssetScrapDetailService.selectAmsAssetScrapDetailList(amsAssetScrapDetail);
        ExcelUtil<AmsAssetScrapDetail> util = new ExcelUtil<AmsAssetScrapDetail>(AmsAssetScrapDetail.class);
        util.exportExcel(response, list, "清理报废明细数据");
        return "";
    }

}
